What is a Java thread?

What is a Java thread?

A thread in Java is the path followed when executing a program. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program’s start, when the main() method is invoked.

In Java, creating a thread is accomplished by implementing an interface and extending a class. Every thread in Java is created and controlled by the java.lang.Thread class.

A single-threaded application has only one Java thread and can handle only one task at a time. To handle multiple tasks in parallel, multi-threading is used: multiple Java threads are created, each performing a different task.


Java thread vs. Java process: what is the difference

A question that often arises is how does a Java thread differ from a Java process.

A Java process is a program in execution. A Java thread is a subset of a Java process.

A Java process consists of multiple threads and a Java thread is often regarded as a light-weight process.

While a Java process has its own address space, a Java thread uses the process’ address space and shares it with other threads of that process. A thread can communicate with other threads of the same process using methods like wait(), notify(), notifyAll(). Global variables can also be used to pass data between threads. On the other hand, a process can only communicate with another process by using inter-process communication techniques such as sockets, files, etc.

Java Thread
Multiple threads of a Java program have their own stack but share the heap memory of the JVM.

Why are multiple threads used in Java applications?

Most commercial Java applications use multi-threading extensively. This is done for several reasons, explained in the multi-thread examples below:

  • For faster processing of background/batch tasks: When multiple tasks must be performed simultaneously, multi-threading allows the different tasks to proceed in parallel. The overall processing time is reduced as a result.
  • To take advantage of modern processors: Most modern systems have multiple processors, and each processor has multiple cores. Multi-threading allows different threads to be run by different processors, thereby allowing the system resources to be more efficiently used.
  • For reducing response times: Users expect Java applications to be fast. By breaking the processing needed for a request into smaller chunks and having different threads handle the processing in parallel, response time can be reduced.
  • To serve multiple users at the same time: Java application servers such as Tomcat, JBoss, Oracle WebLogic and IBM WebSphere are expected to support thousands of users in parallel. Multi-threading is the only way this can be achieved. One Java thread is spawned by the application server for each request to be handled.

Learn more about Java threads

A longer article explaining Java thread is available, here: https://www.eginnovations.com/blog/java-threads/. This article also covers topics such as:

  • Why Should You Monitor Java Threads?
  • Synchronization of Threads Can Also Affect Java Application Performance
  • Monitoring Java Threads: Key Questions to Answer